import { act } from "react-dom/test-utils";
import ACTIONS from "./action";

// 实现运算得到结果
const evaluate = state => {
    // 解构
    let {lastOperand,currentOperand,operation} = state;
    // 浮点数运算
    let last = parseFloat(lastOperand);
    let current = parseFloat(currentOperand);
    
    let res = "";
    switch(operation) {
        // 加法
        case'+':
            res = last + current;
            break;
        case'-':
            res = last - current;
            break;
        case'÷':
            res = last / current;
            break;
        case '×':
            res = last * current;
            break;
    }
    return res.toString();//返回string结果
}

const reducer = (state = {
    currentOperand: "",
    lastOperand: "",
    operand: "",
    overwrite: false,//是否将当前值覆盖掉
},action) => {
    switch(action.type) {
        // 第一种情况,添加数字
        case ACTIONS.ADD_DIGIT:
            // 覆盖数字
            if(state.overwrite){
                return {
                    ...state,
                    currentOperand : action.digit,
                    overwrite: false,
                }
            }
            // 上一位为0，当前这一位也是0，不再重复输出
            if(state.currentOperand === '0' && action.digit === '0'){
                return state;
            }
            // 上一位为0，当前不是点，则删除0
            if(state.currentOperand === '0' && action.digit !== '.'){
                return {
                    ...state,
                    currentOperand: action.digit,
                }
            }
            //已经有.,直接返回，不再重复输出
            if(action.digit === '.' && state.currentOperand === '.'){
                return state;
            }
            // 第一位是.,则补上一位0
            if(action.digit === '.' && state.currentOperand === ''){
                return {
                    ...state,
                    currentOperand: "0."
                }
            }
            return {
                ...state,//后面的关键字会将前面的关键字覆盖掉
                currentOperand: state.currentOperand + action.digit,
            }
        // 第二种情况，删除数字
        case ACTIONS.DELETE_DIGIT:
            if(state.overwrite)
                return {
                    ...state,
                    currentOperand: "",
                    overwrite: false,
                }
            // 已经为空，直接返回
            if(state.currentOperand === "") 
                return state;
            return {
                ...state,
                currentOperand: state.currentOperand.slice(0,-1),
            }
        // 第三种情况，运算符
        case ACTIONS.CHOOST_OPERATION:
            // 没有任何数字操作，直接返回
            if(state.lastOperand === "" && state.currentOperand === "")
                return state;
            // 上面为空，将当前输入放入
            if(state.lastOperand === "")
                return {
                    ...state,
                    lastOperand: state.currentOperand ,
                    operation: action.operation,
                    currentOperand: "",
                }
            // 下面为空，直接将运算符替换
            if(state.currentOperand === "") {
                return {
                    ...state,
                    operation: action.operation,
                }
            }
            // 上下都不为空数字，直接将运算结果存入上面
            return{
                ...state,
                lastOperand: evaluate(state),
                operation: action.operation,
                currentOperand: "",
            }
        // 第四种请况：清空
        case ACTIONS.CLEAR:
            return {
                ...state,
                currentOperand: "",
                lastOperand: "",
                operation: "",
            }
        // 第五种情况：等于结果
        case ACTIONS.EVALUATE:
            // 有一个为空，不操作
            if(state.currentOperand === "" || 
                state.lastOperand === "" || 
                state.operation === "")
                return state;
            return{
                ...state,
                currentOperand : evaluate(state),
                lastOperand: "",
                operation: "",
                overwrite: true,
            }
        // 默认返回原值
        default:
            return state;
    }
};

export default reducer;